package com.xasz.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xasz.entity.Jury;
import com.xasz.entity.Weight;
import com.xasz.service.IWeightService;
import com.xasz.service.ex.DeleteException;
import com.xasz.service.impl.JuryServiceImpl;
import com.xasz.service.impl.WeightServiceImpl;
import com.xasz.util.JsonResult;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cai
 * @since 2022-08-03
 */
@AllArgsConstructor
@RestController
@RequestMapping("weight")
public class WeightController extends BaseController{
    IWeightService iWeightService;
    WeightServiceImpl weightService;
    JuryServiceImpl juryService;
//    /**
//     * 设置权重
//     * 传入评分项集合,以及对应的权重集合
//     */
//    @PostMapping
//    public JsonResult<Void> setWeight(Integer activityId,String[] names,Double[] weights,HttpSession session){
//        weightService.setWeight(activityId,names,weights,getUidFromSession(session));
//        return new JsonResult<>(OK);
//    }
    /**
     * 删除权重
     */
    @DeleteMapping
    public JsonResult<Void> DeleteWeight(@RequestBody Weight weight,HttpSession session){
        weightService.detectWeight(weight,getUidFromSession(session));
        return new JsonResult<>(OK);
    }
    /**
     * 通过活动id查询多维评分的设置
     */
    @GetMapping
    public JsonResult<List<Weight>> getWeight(Integer activityId,HttpSession session){
        //权限判断
        if (!juryService.getOne(new QueryWrapper<Jury>().eq("activity_id",activityId).eq("jury_id",getUidFromSession(session))).getName()
                .equals(getUsernameFromSession(session))){
            throw new DeleteException("你没有权限查看该活动维度信息");
        }
        //查询
        QueryWrapper<Weight> wrapper=new QueryWrapper<Weight>()
                .eq("activity_id",activityId);
        return new JsonResult<>(OK,iWeightService.list(wrapper));
    }
    /**
     * 修改权重信息
     */
    @PutMapping
    public JsonResult<Void> updateWeight(@RequestBody Weight weight,HttpSession session){
        weightService.upDateWeight(weight,getUidFromSession(session));
        return new JsonResult<>(OK);
    }


}
